import math
import random
from eden.ann.neurons import Neuron

if __name__ == "__main__":
	NEURON_CNT = 2
	
	# construct network
	inputNeurons = [Neuron() for i in range(NEURON_CNT)]
	outputNeurons = [Neuron() for i in range(NEURON_CNT)]
	for outputNeuron in outputNeurons:
		for inputNeuron in inputNeurons:
			outputNeuron.addInput(inputNeuron, 0.1)
	
	# run
	for step in range(100):
		# generate samples
		inputs = [random.choice((0, 1)) for i in range(NEURON_CNT)]
		expects = [1 if input == 1 else 0.5 for input in inputs]
		
		# run
		for i in range(NEURON_CNT):
			inputNeurons[i].setVoltage(inputs[i])
		outputs = [None] * NEURON_CNT
		for i in range(NEURON_CNT):
			outputs[i] = outputNeurons[i].run()
		for i in range(NEURON_CNT):
			outputNeurons[i].bp(expects[i])
		
		# statistics
		diff = sum(math.fabs(outputs[i] - expects[i]) for i in range(NEURON_CNT))
		print('STEP#' + str(step) + ': diff=' + str(diff))